﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using AutoRevit.Entry.Command;
using AutoRevit.Event;
using AutoRevit.Tool;

namespace AutoRevitDemo.TestExternalCommand;

[global::Autodesk.Revit.Attributes.Transaction(global::Autodesk.Revit.Attributes.TransactionMode.Manual)]
[global::Autodesk.Revit.Attributes.Journaling(global::Autodesk.Revit.Attributes.JournalingMode.UsingCommandData)]
[global::Autodesk.Revit.Attributes.Regeneration(global::Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class EventCommand : AutoCommand
{
    protected override Result Execute(IRevitData revitData)
    {
        var eeh = ExternalEventHandlerLite.Create();

        eeh.Execute("执行外部事件消息", double.MaxValue, ExternalEventHandler);

        return Result.Succeeded;
    }

    private void ExternalEventHandler(UIApplication uiapp, string data, double number)
    {
        TaskDialog.Show("提示", $"{data}\n{number}");

        var uidoc = uiapp.ActiveUIDocument;

        using (new MouseRightClickFinish())
        {
            if (uidoc.Selection.TryPickObjects(ObjectType.Element, out var rs))
            {
                TaskDialog.Show("选中", $"{string.Join("\n", rs.Select(t => t.ElementId.ToString()))}");
            }
        }
    }
}

[Transaction(TransactionMode.Manual)]
public class InterceptorCommand : AutoCommand, IInterceptorExtend
{
    private class LogInterceptor : IExternalCommandInterceptor
    {
        public HandlerResult Handle(IRevitData revitData)
        {
            Console.WriteLine("拦截器日志写入");
            return new HandlerResult(true);
        }
    }

    public InterceptorCommand()
    {
        Interceptors.Add(new LogInterceptor());
    }

    public List<IExternalCommandInterceptor> Interceptors { get; set; } = [];

    public bool CanInterceptorExecute(IRevitData revitData)
    {
        return true;
    }

    protected override Result Execute(IRevitData revitData)
    {
        return Result.Succeeded;
    }
}

/// <summary>
/// 热更新命令
/// </summary>
[Transaction(TransactionMode.Manual)]
public class HotUpdateCommand : AutoCommand, IHotUpdateExtend
{
    public bool CanUpdate => false;

    protected override Result Execute(IRevitData revitData)
    {
        TaskDialog.Show("title", "这是热更新");
        return Result.Succeeded;
    }
}

[Transaction(TransactionMode.Manual)]
public class PermissionValidationCommand : AuthenticationCommand, IHotUpdateExtend
{
    public bool CanUpdate => true;

    protected override Result Execute(IRevitData revitData)
    {
        TaskDialog.Show("title", "验证通过");
        return Result.Succeeded;
    }

    protected override bool Pass(IRevitData revitData)
    {
        return true;
    }
}